package com.shenda.consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.google.common.eventbus.Subscribe;
import com.shenda.api.service.GyroServiceAsync;
import com.shenda.consumer.event.GyroEvent;
import com.shenda.consumer.event.GyroEvent.GyroEventType;
import com.swak.annotation.FluxReferer;
import com.swak.utils.JsonMapper;

/**
 * 陀螺仪入库
 * 
 * @author lifeng
 */
@Component
public class GyroConsumer {

	private Logger logger = LoggerFactory.getLogger(GyroConsumer.class);

	/**
	 * 数据服务
	 */
	@FluxReferer
	private GyroServiceAsync gyroService;

	/**
	 * 处理消费事件
	 * 
	 * @param data
	 */
	@Subscribe
	public void message(GyroEvent event) {
		if (event.getType() == GyroEventType.更新) {
			gyroService.save(event.getGyro()).whenComplete((v, e) -> {
				if (e != null) {
					logger.error("陀螺仪入库失败:[{}]", JsonMapper.toJson(event.getGyro()));
				}
			});
		} else if (event.getType() == GyroEventType.檫除) {
			gyroService.updateFlush(event.getGyro()).whenComplete((v, e) -> {
				if (e != null) {
					logger.error("陀螺仪檫除失败:[{}]", JsonMapper.toJson(event.getGyro()));
				}
			});
		} else if (event.getType() == GyroEventType.设置) {
			gyroService.updateSettings(event.getGyro()).whenComplete((v, e) -> {
				if (e != null) {
					logger.error("陀螺仪设置失败:[{}]", JsonMapper.toJson(event.getGyro()));
				}
			});
		}
	}
}